﻿/* ######################################## *\
 * ### Copyright (C) 2009 AJ Ravindiran ### *
\* ######################################## */
using System;
using System.IO;
using System.Reflection;

using AJRavindiran.Jolt.Utilities.Parsing;

namespace AJRavindiran.Jolt
{
    /// <summary>
    /// Represents the start point of the environment.
    /// </summary>
    public static class Program
    {
        #region Fields
        /// <summary>
        /// Determines if the server should run in debug mode.
        /// </summary>
        public static bool DebugMode = false;

        /// <summary>
        /// True if all errors should be written to a log file.
        /// </summary>
        public static bool WriteEnabled = true;
        #endregion Fields

        #region Methods
        /// <summary>
        /// This is the main method, this application will start at this method.
        /// </summary>
        public static void Main(string[] args)
        {
            // Load and configure console settings.
            try
            {
                Xml xml = new Xml("system_console.xml");

                DebugMode = xml.ReadBoolean("console.debugmode");
                WriteEnabled = xml.ReadBoolean("console.logtofile");

                if (xml.ReadBoolean("console.resize"))
                {
                    Console.WindowHeight = xml.ReadInt32("console.height");
                    Console.WindowWidth = xml.ReadInt32("console.width");
                }

                xml = null;
            }
            catch
            {
                new Utilities.Debug.Log().WriteError("Could not configure console settings.");
                Console.ReadKey(false);
                Environment.Exit(0);
            }

            Console.Title = "Jolt Environment [Garbage Collection: " + GC.GetTotalMemory(false) / 1024 + "KB]";

            // DO NOT REMOVE/EDIT THIS BANNER.
            Console.WriteLine("######################################################################");
            Console.WriteLine("### ---------------------- Jolt Environment ---------------------- ###");
            Console.WriteLine("### -------------- Copyright (C) 2009 AJ Ravindiran -------------- ###");
            Console.WriteLine("######################################################################");
            Console.WriteLine();
            Console.WriteLine();

            // Load all drivers for the environment.
            AppDomain.CurrentDomain.AssemblyResolve += FindDrivers;

            // Initialize Jolt Environment
            Jolt.Initialize();

            while (true)
            {
                string input = Console.ReadLine().Trim();
                string command = input.Split(' ')[0];
                string[] arguments = input.Substring(command.Length).Trim().Split(' ');
                Utilities.CommandHandler.Handle(command, arguments);
            }
        }

        /// <summary>
        /// Loads all drivers enlisted in \drivers\ directory.
        /// </summary>
        private static Assembly FindDrivers(object sender, ResolveEventArgs args)
        {
            string drivers = new AssemblyName(args.Name).Name;
            string path = @".\bin\drivers\" + drivers + ".dll";

            if (!File.Exists(path)) return null;
            return Assembly.LoadFrom(path);
        }
        #endregion Methods
    }
}
